package com.tsd.jxc.order.service.impl;

import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.buy.dao.JxcBuyOrderDetailMapper;
import com.tsd.jxc.buy.dao.JxcBuyOrderMapper;
import com.tsd.jxc.fin.FinanceConstants;
import com.tsd.jxc.order.dao.JxcOrderCutMoneyMapper;
import com.tsd.jxc.order.entity.JxcOrderCutMoney;
import com.tsd.jxc.order.entity.JxcOrderCutMoneyExt;
import com.tsd.jxc.order.service.JxcOrderCutMoneyService;
import com.tsd.jxc.sale.dao.JxcSaleOrderDetailMapper;
import com.tsd.jxc.sale.dao.JxcSaleOrderMapper;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcOrderCutMoney 服务实现层
 *
 * @author Hillpool
 * @date 2023/03/02 13:47:24
 */
@Service
public class JxcOrderCutMoneyServiceImpl extends BaseServiceImpl implements JxcOrderCutMoneyService {

    @Resource
    private JxcOrderCutMoneyMapper jxcOrderCutMoneyMapper;
    @Resource
    private JxcSaleOrderDetailMapper jxcSaleOrderDetailMapper;
    @Resource
    private JxcSaleOrderMapper jxcSaleOrderMapper;
    @Resource
    private JxcBuyOrderDetailMapper jxcBuyOrderDetailMapper;
    @Resource
    private JxcBuyOrderMapper jxcBuyOrderMapper;

    @Override
    public JxcOrderCutMoney getJxcOrderCutMoneyById(Long id) throws Exception {
        return jxcOrderCutMoneyMapper.selectByPrimaryKey(id);
    }

    @Override
    public JxcOrderCutMoneyExt queryBySid(String sid) throws Exception {
        return jxcOrderCutMoneyMapper.selectBySid(sid);
    }

    @Override
    public void deleteJxcOrderCutMoneys(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            JxcOrderCutMoney old = jxcOrderCutMoneyMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            jxcOrderCutMoneyMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveJxcOrderCutMoney(JxcOrderCutMoneyExt record, SysUser opUser) throws Exception {
        super.checkEmpty(record.getType(), "缺少参数：type");
        super.checkEmpty(record.getOrder_sid(), "缺少参数：order_sid");
        super.checkEmpty(record.getOrder_detail_sid(), "缺少参数：order_detail_sid");
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            jxcOrderCutMoneyMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            jxcOrderCutMoneyMapper.updateByPrimaryKeySelective(record);
        }
        if (FinanceConstants.TYPE_SALE.equals(record.getType())) {
            jxcSaleOrderDetailMapper.updateCutAmountBySidList(Collections.singletonList(record.getOrder_detail_sid()));
            jxcSaleOrderMapper.updateCutAmountBySidList(Collections.singletonList(record.getOrder_sid()));
        } else {
            jxcBuyOrderDetailMapper.updateCutAmountBySidList(Collections.singletonList(record.getOrder_detail_sid()));
            jxcBuyOrderMapper.updateCutAmountBySidList(Collections.singletonList(record.getOrder_sid()));
        }
    }

    @Override
    public List<JxcOrderCutMoneyExt> findJxcOrderCutMoneys(Map<String, Object> params, PageBean page) throws Exception {
        return jxcOrderCutMoneyMapper.selectByPage(params, page);
    }

    @Override
    public List<JxcOrderCutMoneyExt> findJxcOrderCutMoneys(Map<String, Object> params) throws Exception {
        return jxcOrderCutMoneyMapper.selectByParams(params);
    }
}